import pandas as pd

# 读取数据
data = pd.read_csv(r"C:\Users\桐桐\Downloads\youtube_channel_real_performance_analytics.csv")

# 检查缺失值
print(data.isnull().sum())

# 处理缺失值，这里我们选择填充0，具体方法根据实际情况选择
data.fillna(0, inplace=True)

# 假设我们关注的是Views和Revenue，我们可以设置一个阈值来检测异常值
data['Views'] = pd.to_numeric(data['Views'], errors='coerce')
data['Estimated Revenue (USD)'] = pd.to_numeric(data['Estimated Revenue (USD)'], errors='coerce')

# 定义异常值的阈值
upper_limit_views = data['Views'].quantile(0.99)
upper_limit_revenue = data['Estimated Revenue (USD)'].quantile(0.99)

# 标记异常值
data['异常_Views'] = data['Views'] > upper_limit_views
data['异常_Revenue'] = data['Estimated Revenue (USD)'] > upper_limit_revenue

# 查看异常值
print(data[data['异常_Views'] | data['异常_Revenue']])
# 从发布日期中提取星期几
data['Publish Date'] = pd.to_datetime(data['Video Publish Time'])
data['Day of Week'] = data['Publish Date'].dt.day_name()

# 检查是否为节假日（这里需要一个节假日的列表，示例中省略）
# data['Is Holiday'] = data['Publish Date'].isin(holidays_list)
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns


# 设置全局图表风格
sns.set(style="whitegrid")

# 1. 观看次数分布图 (Histogram)
plt.figure(figsize=(10, 6))
sns.histplot(data['Views'], bins=30, kde=True, color="skyblue")
plt.title("Distribution of Video Views")
plt.xlabel("Views")
plt.ylabel("Frequency")
plt.show()

# 2. 收入与观看次数的关系 (Scatter Plot)
plt.figure(figsize=(10, 6))
sns.scatterplot(x='Views', y='Estimated Revenue (USD)', data=data, color="green")
plt.title("Relationship between Views and Estimated Revenue")
plt.xlabel("Views")
plt.ylabel("Estimated Revenue (USD)")
plt.show()

# 3. 曝光次数与点击率的关系 (Scatter Plot)
plt.figure(figsize=(10, 6))
sns.scatterplot(x='Impressions', y='Video Thumbnail CTR (%)', data=data, color="orange")
plt.title("Impressions vs. Video Thumbnail Click-Through Rate")
plt.xlabel("Impressions")
plt.ylabel("Video Thumbnail CTR (%)")
plt.show()

# 4. 每月上传趋势 (Line Plot)
# 确保数据集有'Month'列，可以使用日期列提取月份
monthly_trend = data.groupby('Month').size()

plt.figure(figsize=(10, 6))
sns.lineplot(x=monthly_trend.index, y=monthly_trend.values, marker="o", color="purple")
plt.title("Monthly Video Upload Trend")
plt.xlabel("Month")
plt.ylabel("Number of Videos")
plt.xticks(range(1, 13))  # 显示12个月
plt.show()


# 选择相关变量和目标变量
target_variable = 'Views'
related_variables = [
    'Video Duration', 
    'Days Since Publish', 
    'Stream Hours', 
    'Watch Time (hours)', 
    'Impressions', 
    'Video Thumbnail CTR (%)'
]

# 构建相关系数矩阵，只包括数值类型的列
correlation_matrix = data[[target_variable] + related_variables].corr()

# 创建热力图
plt.figure(figsize=(12, 10))
sns.heatmap(correlation_matrix, annot=True, cmap='coolwarm', fmt=".2f", square=True)
plt.title('Correlation Heatmap for YouTube Video Performance Analytics')
plt.xticks(rotation=45)
plt.yticks(rotation=0)
plt.show()


# 选择相关变量和目标变量
target_variable = 'Views'
related_variables = [
    'Watch Time (hours)', 
    'Impressions', 
    'Video Thumbnail CTR (%)'
]

# 构建相关系数矩阵，只包括数值类型的列
correlation_matrix = data[[target_variable] + related_variables].corr()

# 创建热力图
plt.figure(figsize=(12, 10))
sns.heatmap(correlation_matrix, annot=True, cmap='coolwarm', fmt=".2f", square=True)
plt.title('Correlation Heatmap for YouTube Video Performance Analytics')
plt.xticks(rotation=45)
plt.yticks(rotation=0)
plt.show()

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler, MinMaxScaler

# ---------------------------
# 1. 读取数据
# ---------------------------
# 假设数据存储在 'data.csv' 文件中
df = pd.read_csv(r"c:\Users\桐桐\Downloads\youtube_channel_real_performance_analytics.csv")

# 查看数据前5行
print("数据前5行：")
print(df.head())

# ---------------------------
# 2. 数据检查与清洗
# ---------------------------
# a. 检查缺失值
print("\n缺失值统计：")
print(df.isnull().sum())

# 使用中位数填充数值列的缺失值
df['Watch Time (hours)'].fillna(df['Watch Time (hours)'].median(), inplace=True)
df['Impressions'].fillna(df['Impressions'].median(), inplace=True)
df['Video Thumbnail CTR (%)'].fillna(df['Video Thumbnail CTR (%)'].median(), inplace=True)

# b. 检查重复值
print("\n重复值统计：", df.duplicated().sum())
df.drop_duplicates(inplace=True)

# c. 检查异常值 (使用箱线图可视化)
plt.figure(figsize=(12, 6))
sns.boxplot(data=df[['Watch Time (hours)', 'Impressions', 'Video Thumbnail CTR (%)', 'Views']])
plt.title("Boxplot of Features")
plt.show()

# 使用四分位距法去除异常值
def remove_outliers(df, column):
    Q1 = df[column].quantile(0.25)
    Q3 = df[column].quantile(0.75)
    IQR = Q3 - Q1
    lower_bound = Q1 - 1.5 * IQR
    upper_bound = Q3 + 1.5 * IQR
    return df[(df[column] >= lower_bound) & (df[column] <= upper_bound)]

# 对相关变量去除异常值
for col in ['Watch Time (hours)', 'Impressions', 'Video Thumbnail CTR (%)', 'Views']:
    df = remove_outliers(df, col)

# ---------------------------
# 3. 目标变量和特征处理
# ---------------------------
# a. 目标变量 - 观看次数（Views）的分布检查和对数变换
plt.figure(figsize=(8, 4))
sns.histplot(df['Views'], bins=30, kde=True, color='blue')
plt.title("Distribution of Views (Original)")
plt.show()

# 对目标变量进行对数变换，减少分布偏态
df['Views_log'] = np.log1p(df['Views'])

plt.figure(figsize=(8, 4))
sns.histplot(df['Views_log'], bins=30, kde=True, color='green')
plt.title("Distribution of Views (Log Transformed)")
plt.show()

# b. 特征工程 - 对数变换相关变量
df['Impressions_log'] = np.log1p(df['Impressions'])
df['Watch Time (hours)_log'] = np.log1p(df['Watch Time (hours)'])

# ---------------------------
# 4. 特征缩放与标准化
# ---------------------------
# 提取特征和目标变量
features = ['Watch Time (hours)_log', 'Impressions_log', 'Video Thumbnail CTR (%)']
X = df[features]
y = df['Views_log']

# 使用StandardScaler进行标准化
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

# 将标准化后的数据转换为DataFrame
X_scaled = pd.DataFrame(X_scaled, columns=features)
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LinearRegression
from sklearn.ensemble import RandomForestRegressor
from xgboost import XGBRegressor
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score

# 读取数据
data = pd.read_csv(r"C:\Users\桐桐\Downloads\youtube_channel_real_performance_analytics.csv")

# 选择目标变量和相关变量
target = 'Views'
features = ['Watch Time (hours)', 'Impressions', 'Video Thumbnail CTR (%)']

# 选取需要的列
data = data[features + [target]]

# **处理缺失值（填充中位数）**
data = data.fillna(data.median())

# 分割特征和目标变量
X = data[features]
y = data[target]

# 拆分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# 数据标准化
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
# 定义多个模型
models = {
    'Linear Regression': LinearRegression(),
    'Random Forest': RandomForestRegressor(n_estimators=100, random_state=42),
    'XGBoost': XGBRegressor(n_estimators=500, learning_rate=0.1, max_depth=4, random_state=42)
}

# 训练和评估模型
results = {}
for model_name, model in models.items():
    # 训练模型
    model.fit(X_train, y_train)
    # 预测
    y_pred = model.predict(X_test)
    # 评估指标
    mse = mean_squared_error(y_test, y_pred)
    mae = mean_absolute_error(y_test, y_pred)
    r2 = r2_score(y_test, y_pred)
    # 保存结果
    results[model_name] = {'MSE': mse, 'MAE': mae, 'R²': r2}

# 打印比较结果
print("模型比较结果：")
for model_name, metrics in results.items():
    print(f"\n{model_name}:")
    print(f"均方误差 (MSE): {metrics['MSE']:.2f}")
    print(f"平均绝对误差 (MAE): {metrics['MAE']:.2f}")
    print(f"R²: {metrics['R²']:.2f}")

import matplotlib.pyplot as plt
import seaborn as sns

# 模型性能指标
models = ['Linear Regression', 'Random Forest', 'XGBoost']
mse = [2053806061.46, 926818716.93, 952610980.14]
mae = [27606.00, 18459.08, 19115.11]
r2 = [0.88, 0.94, 0.94]

# 设置 Seaborn 样式
sns.set(style="whitegrid")

# 图表 1：MSE 对比
plt.figure(figsize=(12, 6))
plt.subplot(1, 3, 1)
sns.barplot(x=models, y=mse, palette="viridis")
plt.title("MSE (Mean Squared Error)")
plt.ylabel("MSE")
plt.xticks(rotation=45)

# 图表 2：MAE 对比
plt.subplot(1, 3, 2)
sns.barplot(x=models, y=mae, palette="plasma")
plt.title("MAE (Mean Absolute Error)")
plt.ylabel("MAE")
plt.xticks(rotation=45)

# 图表 3：R² 对比
plt.subplot(1, 3, 3)
sns.barplot(x=models, y=r2, palette="magma")
plt.title("R² (R-Squared)")
plt.ylabel("R² Score")
plt.ylim(0, 1)  # R² 范围 [0, 1]
plt.xticks(rotation=45)

# 调整布局
plt.tight_layout()
plt.show()
# 导入必要的库
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score
import matplotlib
import matplotlib.pyplot as plt

# 设置 Matplotlib 支持中文
matplotlib.rcParams['font.family'] = 'SimHei'  # 使用黑体字体
matplotlib.rcParams['axes.unicode_minus'] = False  # 解决负号 '-' 显示问题

# 数据预处理
# 选择目标变量和相关变量
target = 'Views'
features = ['Watch Time (hours)', 'Impressions', 'Video Thumbnail CTR (%)']

# 筛选相关列
data = data[[target] + features]

# 转换数值类型并处理缺失值
data = data.apply(pd.to_numeric, errors='coerce')  # 将无法解析的数据转换为NaN
data = data.dropna()  # 删除缺失值的行

#  数据拆分
X = data[features]  # 特征变量
y = data[target]    # 目标变量

# 将数据集拆分为训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

#  模型训练 - Random Forest
rf_model = RandomForestRegressor(n_estimators=100, random_state=42)
rf_model.fit(X_train, y_train)

#  预测
y_pred = rf_model.predict(X_test)

# 模型评估
mse = mean_squared_error(y_test, y_pred)
mae = mean_absolute_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)

print("Random Forest 模型表现：")
print(f"均方误差 (MSE): {mse:.2f}")
print(f"平均绝对误差 (MAE): {mae:.2f}")
print(f"决定系数 (R²): {r2:.2f}")

# 导入必要的库
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
import shap  # SHAP 解释库
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score

# 设置 Matplotlib 支持中文
matplotlib.rcParams['font.family'] = 'SimHei'  # 使用黑体字体
matplotlib.rcParams['axes.unicode_minus'] = False  # 解决负号 '-' 显示问题

#  SHAP 解释模型
explainer = shap.TreeExplainer(rf_model)  # 创建 SHAP Explainer
shap_values = explainer.shap_values(X_test)  # 计算 SHAP 值

# 总体特征重要性解释（条形图）
plt.figure(figsize=(8, 6))
shap.summary_plot(shap_values, X_test, plot_type="bar", show=False)  # 条形图展示
plt.title("SHAP 特征重要性 - Random Forest")
plt.show()

# 单个样本的 SHAP 力图
sample_idx = 10  # 测试集中的第 10 个样本
shap.force_plot(explainer.expected_value, shap_values[sample_idx, :], X_test.iloc[sample_idx, :], matplotlib=True)
plt.show()

# 可视化 - 实际值 vs 预测值的散点图
plt.figure(figsize=(10, 6))
plt.scatter(y_test, y_pred, alpha=0.7, color='blue', label='预测值')  # 预测值
plt.plot([min(y_test), max(y_test)], [min(y_test), max(y_test)], color='red', linestyle='--', label='理想值')  # 理想对角线
plt.xlabel("实际观看量 (Views)")
plt.ylabel("预测观看量 (Views)")
plt.title("实际值 vs 预测值 散点图")
plt.legend()
plt.grid()
plt.show()